/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */
// class TreeNode {
//     val: number
//     left: TreeNode | null
//     right: TreeNode | null
//     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
//         this.val = (val === undefined ? 0 : val)
//         this.left = (left === undefined ? null : left)
//         this.right = (right === undefined ? null : right)
//     }
// }

function nodeEqual(a: TreeNode | null, b: TreeNode | null): boolean {
    if (b == null) {
        return true;
    }
    if (a == null || a.val != b.val) {
        return false;
    }
    return nodeEqual(a.left, b.left) && nodeEqual(a.right, b.right);

}

function isSubStructure(A: TreeNode | null, B: TreeNode | null): boolean {
    if (A == null || B == null) {
        return false;
    }

    let a = A;
    let b = B;
    let list = new Array<TreeNode>();

    list.push(a);
    while (list.length > 0) {
        let node = list.pop();
        if (node != null) {
            if (node.val == b.val) {
                if (nodeEqual(node, b)) {
                    return true;
                }
            }
            if (node.left) {
                list.push(node.left);
            }
            if (node.right) {
                list.push(node.right);
            }

        }
    }




    return false;
};